<?php

/**
 * Subclass for representing a row from the 'salarie' table.
 *
 *
 *
 * @package lib.model
 */
class Salarie extends BaseSalarie {

  public function __toString() {
    return $this->getNomComplet();
  }

  /*
   * Renvoie le nom complet prenom + nom
   * met une majuscule � chacun des deux termes
   * 
   * @param: int ( 1 default : renvoie prenom + nom , 2 renvoie nom + prenom
   * @return: string nom complet 
   */

  public function getNomComplet($mode = 1) {
    if ($mode == 2)
      return ucfirst($this->getNom()) . ' ' . ucfirst($this->getPrenom());
    return ucfirst($this->getPrenom()) . ' ' . ucfirst($this->getNom());
  }

  public function getNombreCA() {
      $objDateDeb = new clDate(date('Y').'-01-04');
      $objDateFin = new clDate('2016-01-03');
      $objDateJour = clDate::makeDateToday();
      $c = new Criteria();
      $crit0 = $c->getNewCriterion(JourPeer::WORKER_ID, $this->getId());
      $crit1 = $c->getNewCriterion(JourPeer::DATE, $objDateDeb->getDate(), Criteria::GREATER_EQUAL);
      $crit2 = $c->getNewCriterion(JourPeer::DATE, $objDateFin->getDate(), Criteria::LESS_EQUAL);
      $crit0->addAnd($crit1);
      $crit0->addAnd($crit2);
      $c->add($crit0);
      $tab = JourPeer::doSelect($c);
      $nombreCa = 0;
      $nombreCaPris = 0;
      $libCa = 'Liste des jours de CA posés en '.date('Y').' : ';
      $libCaPris = 'Liste des jours de CA déjà pris en '.date('Y').' : ';
      foreach ($tab as $elem) {
          if (($elem->isValide() OR $elem->isValideResponsable()) AND $elem->getType() == 4) {
              $nombreCa++;
              $objDateElem = new clDate($elem->getDate());
              $libCa .= $objDateElem->getDate('d/m/Y').' - ';
              if ( $objDateJour->getTimestamp() >= $objDateElem->getTimestamp() ) {
                  $nombreCaPris++;
                  $libCaPris .= $objDateElem->getDate('d/m/Y').' - ';
              }
          }
      }
      return array('total'=>$nombreCa,'pris'=>$nombreCaPris,'restant'=>$nombreCa-$nombreCaPris,'lib_total'=>$libCa,'lib_pris'=>$libCaPris);
  }

  public function getDeltaTotal($dateDeb,$dateFin='') {
    if(!$this->getDateEmbauche())
      throw new Exception("Aucune date d'embauche n'a été renseignée pour ce salarié !");
    
    $today = clDate::makeDateToday();
    $objDateDeb = new clDate($dateDeb);
    if ( $dateFin ) {
        $objDateFin = new clDate($dateFin);
    } else {
        $objDateFin = clDate::makeDateToday();
    }
    //print "FINFINFINFIN".$objDateFin->getDate();
    $embauche = new clDate($this->getDateEmbauche());
    
    if ( $embauche->getTimestamp() > $objDateDeb->getTimestamp())
        $dateDebutCalcul = $embauche;
    else $dateDebutCalcul = $objDateDeb;
    
    $c = new Criteria();


    $crit0 = $c->getNewCriterion(JourPeer::WORKER_ID, $this->getId());
    $crit1 = $c->getNewCriterion(JourPeer::DATE, $dateDebutCalcul->getDate(), Criteria::GREATER_EQUAL);
    $crit2 = $c->getNewCriterion(JourPeer::DATE, $objDateFin->getDate(), Criteria::LESS_EQUAL);
    //$crit2 = $c->getNewCriterion(JourPeer::DATE, $dateFinSOP->getDate(), Criteria::LESS_EQUAL);
    //$crit3 = $c->getNewCriterion(JourPeer::IS_VALIDE, 1);

    $crit0->addAnd($crit1);
    $crit0->addAnd($crit2);
    //$crit0->addAnd($crit3);

    $c->add($crit0);

    $tab = JourPeer::doSelect($c);
    
    $total = 0;
    $nb_astreinte = 0;
    $total_astreinte = 0;
      $lib_astreinte = '';
    foreach ($tab as $elem) {
      if ( $elem->getDate() == $today->getDate() && !$elem->isValide() && !$elem->isValideResponsable() && $elem->getType() == 1 ) {
          $total += (($this->getNbHeuresSemaine()*60)/5);
      } else {  
        if ($elem->getIsCredit() && ($elem->isValide() OR $elem->isValideResponsable()) )
          $total -= $elem->getTotalJournee();
        elseif ( ( $elem->isValide() OR $elem->isValideResponsable() ) AND $elem->getType() != 3 )
          $total += $elem->getTotalJournee();
        elseif ( ( $elem->isValide() OR $elem->isValideResponsable() ) AND $elem->getType() == 3 ) {
          $nb_astreinte ++;
            $objDateElem = new clDate($elem->getDate());
            $lib_astreinte .= $objDateElem->getDate('d/m/Y').' - ';
          $total_astreinte += $elem->getTotalJournee();
        }
      }
    }
    //$fin = clDate::makeDateToday();
    $fin = $objDateFin;
    $fin->addDays(1);
    $nbJour = 0;
    while ($dateDebutCalcul->getDatetime() != $fin->getDatetime()) {
      if ($dateDebutCalcul->getDayWeekNumber() != 6 && $dateDebutCalcul->getDayWeekNumber() != 0)
        $nbJour += 1;
      $dateDebutCalcul->addDays(1);
    }

    $nbHMois = $nbJour * ($this->getNbHeuresSemaine() / 5) * 60;

    $final = $total - $nbHMois;
    $max = 41.5*60*($this->getNbHeuresSemaine()/35);
      $avertissement = '';
    if ( $final > $max ) $avertissement = ' (limite maximum de '.min2rep($max).' heures supplémentaires par cycle de 12 semaines dépassée)';
    return "Delta : ".min2rep($final).$avertissement." - Jours d'astreinte : $nb_astreinte - Interventions : ".min2rep($total_astreinte);
  }

  public function getDeltaMensuel($d) {

    $m = substr($d, 5, 2);
    $a = substr($d, 0, 4);

    $a1 = ($m == 12 ? $a + 1 : $a);
    $m1 = $m + 1;
    $m1 = ($m1 == 13 ? 1 : $m1);
    $m1 = ($m1 < 10 ? "0" . $m1 : "" . $m1);
    $c = new Criteria();
    $crit0 = $c->getNewCriterion(JourPeer::WORKER_ID, $this->getId());
    $crit1 = $c->getNewCriterion(JourPeer::DATE, $a . "-" . $m . "-01", Criteria::GREATER_EQUAL);
    $crit2 = $c->getNewCriterion(JourPeer::DATE, $a1 . "-" . $m1 . "-01", Criteria::LESS_THAN);
    $crit3 = $c->getNewCriterion(JourPeer::IS_VALIDE, 1);

    $crit0->addAnd($crit1);
    $crit0->addAnd($crit2);
    $crit0->addAnd($crit3);

    $c->add($crit0);
    $jours = JourPeer::doSelect($c);
    $total = 0;
    $nbJour = 0;
    foreach ($jours as $jour) {
      $total += $jour->getTotalJournee();
    }
    $debutM = new clDate("01/" . $m . "/" . $a);
    $finM = new clDate("01/" . $m1 . "/" . $a1);
    while ($debutM->getDatetime() != $finM->getDatetime()) {
      if ($debutM->getDayWeekNumber() != 6 && $debutM->getDayWeekNumber() != 0)
        $nbJour += 1;
      $debutM->addDays(1);
    }
    $nbHMois = $nbJour * ($this->getNbHeuresSemaine() / 5) * 60 * -1;

    $final = $nbHMois + $total;
    return $final;
  }

  public function getNomPrepose() {
    $firstChar = substr($this->getPrenom(), 0, 1);
    if (strpos('aeiouyAEIOUY', $firstChar))
      return "d'" . $this->getPrenom() . " " . $this->getNom();
    return "de " . $this->getPrenom() . " " . $this->getNom();
  }

  public function getGuardString() {
    if ($this->getGuardId() == 0)
      return "";
    $username = sfGuardUserPeer::retrieveByPK($this->getGuardId());
    if (!$username)
      return null;
    else
      return $username->getUsername();
  }

  public function isUniteFonctionnelleExists() {
    return count(UniteFonctionnellePeer::doSelect(new Criteria()));
  }

  public function getUniteFonctionnelleAssociees() {
    $tab_unite_fonctionnelle = array();

    foreach ($this->getAssoSalarieUniteFonctionnelles() as $asso_salarie_unite_fonctionnelle)
      array_push($tab_unite_fonctionnelle, $asso_salarie_unite_fonctionnelle->getUniteFonctionnelle());

    return $tab_unite_fonctionnelle;
  }
  
  public function isResponsable(UniteFonctionnelle $uf) {
    return (AssoSalarieUniteFonctionnellePeer::retrieveByPK($this->getPrimaryKey(), $uf->getPrimaryKey()) instanceof AssoSalarieUniteFonctionnelle
      && AssoSalarieUniteFonctionnellePeer::retrieveByPK($this->getPrimaryKey(), $uf->getPrimaryKey())->getIsResponsable());
  }
  
  public function isPartOfUniteFonctionnelle(UniteFonctionnelle $uf) {
    foreach($this->getAssoSalarieUniteFonctionnelles() as $asso)
      if($asso->getUfId() == $uf->getPrimaryKey())
        return true;
    
    return false;
  }

}
